package org.example.schoolbus.controller;

import org.example.schoolbus.entity.ResultBody;
import org.example.schoolbus.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Collectionmessage")
@CrossOrigin(origins="*")
public class CollectionController {
    @Autowired
    private CollectionService collectionService;

    @RequestMapping("/addcollecton")
    public ResultBody addcollecton(@RequestBody Map<String, Object> map) {
        //进行参数有无判断
        if (map.get("userphone") == null) {
            return new ResultBody(400, "缺少参数", false);
        }
        else if (map.get("station_name") == null && map.get("route_name") == null) {
            return new ResultBody(400, "缺少参数", false);
        }
        //当有电话号码时,首先判断号码是否存在
        String userphone=map.get("userphone").toString();
        if (collectionService.puserphone(userphone)==0) {
            return new ResultBody(400,"电话号码传值错误",false);
        } else {
        //站点查询
            if (map.get("station_name") != null) {
            String station_name = map.get("station_name").toString();
            if (collectionService.addcollection(userphone, station_name,"empty","站点") >0) {
                return new ResultBody(200, "收藏成功", true, map.size());
            }
        }
        //线路查询
       else  if(map.get("route_name") != null) {
                 String route_name = map.get("route_name").toString();
                 if (collectionService.addcollection(userphone,"empty",route_name,"路线") > 0) {
                return new ResultBody(200, "收藏成功", true, map.size());
                 }
            }
        }
            return new ResultBody(400,"收藏失败",false);
    }

    @RequestMapping("/lookcollection")
    public ResultBody lookcollection(@RequestBody Map<String, Object> map) {
        //判断参数是否存在
        if (map.get("userphone") == null||map.get("collection_style") == null) {
            return new ResultBody(404, "缺少参数", false);
        }
        String userphone = map.get("userphone").toString();
        String collection_style = map.get("collection_style").toString();
        List<Map<String, Object>> lists = collectionService.lookcollections(userphone);
        List<Map<String, Object>> listr = collectionService.lookcollectionr(userphone);
        //判断前端传入的参数是否有无
        if (collectionService.luserphone(userphone) == 0) {
            return new ResultBody(400, "电话号码错误", false);
        } else if (collectionService.luserphone(userphone) > 0) {
           if(collection_style.equals("站点")) {
               //查看站点收藏
               if(lists.size()>0||lists!=null) {
                   //查看成功
                   return new ResultBody(200,"查看收藏成功",lists);
               }
               else
                   return new ResultBody(400, "查询失败", false);
           }
           else if(collection_style.equals("路线")){
               //查看路线收藏
               if(listr.size()>0||listr!=null) {
                   //查看成功
                   return new ResultBody(200,"查看收藏成功",listr);
               }
               else
                   //查看失败
                   return new ResultBody(400, "查询失败", false);
           }
        }
            return new ResultBody(400, "查询失败", false);
    }
}
